<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 教练列表
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                        type="primary"
                        class="handle-del mr10"
                        @click="delAllSelection"
                >批量删除</el-button>
                <el-button
                        type="primary"
                        class="handle-del mr10"
                        @click="exportAll"
                >导出所有</el-button>
                <el-input v-model="query.name" placeholder="教练姓名" class="handle-input mr10" style="width: 15%"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="userid" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="username" label="教练姓名"></el-table-column>
                <el-table-column prop="userpassword" label="密码"></el-table-column>
                <el-table-column prop="usersex" label="性别"></el-table-column>
                <el-table-column prop="usertel" label="手机号"></el-table-column>
                <el-table-column prop="useremail" label="邮箱"></el-table-column>
<!--                <el-table-column prop="opertor" label="添加人"></el-table-column>-->
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%" :before-close="cancelEdit">
            <el-form ref="form"  label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.username"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="form.userpassword"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-select v-model="form.usersex" placeholder="请选择" style="width: 40%">
                        <el-option  label="男" value="男"></el-option>
                        <el-option  label="女" value="女"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="form.usertel"></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="form.useremail"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancelEdit">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {deleteUserByIds,exportAllTeacher, delUser, getTeaList, updateUserById} from '../../api/admin';
    export default {
        name: 'basetable',
        data() {
            return {
                query: {
                    name: '',
                    pageIndex: 1,
                    pageSize: 5
                },
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                pageTotal: 0,
                form: {},
                idx: -1,
                id: -1
            };
        },
        created() {
            this.getData();
        },
        methods: {
            // 获取 easy-mock 的模拟数据
            getData() {
                getTeaList(this.query).then(res => {
                    console.log(res);
                    this.tableData = res.list;
                    this.pageTotal = res.totals;
                    this.query.name=null;
                });
            },
            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, 'pageIndex', 1);
                this.getData();
            },
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        delUser(row).then((res) =>{
                            console.log(res);
                            if(res.state == 2000){
                                this.$message.success('删除成功');
                                this.tableData.splice(index, 1);
                                //this.clear();
                            } else {
                                this.$message.error(res.message);
                            }
                        })
                    });
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
                console.log(this.multipleSelection)
            },
            delAllSelection() {
                const length = this.multipleSelection.length;
                let ids = new Array();
                for (let i = 0; i < length; i++) {
                    ids.push(this.multipleSelection[i].userid);
                }
                console.log(ids);

                deleteUserByIds(ids).then((res) => {
                    if(res.state == 2000){
                        this.$message.success('删除成功');
                        this.getData();
                    } else {
                        this.$message.error(res.message);
                    }
                })
                this.multipleSelection = [];
            },
            // 编辑操作
            handleEdit(index, row) {
                this.idx = index;
                this.form = row;
                this.editVisible = true;
            },
            //取消编辑
            cancelEdit() {
                this.editVisible = false;
                this.form.username='';
                this.form.userpassword='';
                this.form.usersex='';
                this.form.usertel='';
                this.form.useremail='';
                this.getData();
                //this.$set(this.tableData, this.idx, this.form);
            },
            exportAll(){
                let serverUrl = "http://localhost:8092/yoga"
                window.location.href= serverUrl+"/user/exportAllUser?type=2"
                // exportAllTeacher().then(res => {
                //     // let blob = new Blob([res.data], {
                //     //      //type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8"
                //     //     type: 'application/vnd.ms-excel'
                //     //    });
                //     // console.log(blob)
                //     // let url = window.URL.createObjectURL(blob);
                //     // console.log(blob);
                //     // const a = document.createElement("a");
                //     // document.body.appendChild(a);
                //     // a.href = url;
                //     // a.download ="教练名单.xls";
                //     // a.click();
                //     // window.URL.revokeObjectURL(url);
                //
                // });
            },
            // 保存编辑
            saveEdit() {
                // this.editVisible = false;
                // this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                // this.$set(this.tableData, this.idx, this.form);
                this.editVisible = false;
                updateUserById(JSON.stringify({'userid':this.form.userid,'username':this.form.username,'userpassword':this.form.userpassword,'usersex':this.form.usersex,'usertel':this.form.usertel,'useremail':this.form.useremail})).then(res =>{
                    this.getData();
                    console.log(res);
                    this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                })
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
